import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class CadastrarPessoaTest {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost:8080/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testCadastrarPessoa() throws Exception {
    driver.get(baseUrl + "/AgendaVirtual/faces/agenda.jsp");
    driver.findElement(By.xpath("//*[@id=\"agendaForm:addPessoa\"]")).click();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:nome\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:nome\"]")).sendKeys(String.valueOf(Aleatorio.getRandomObject(Type.NOMES)));
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:sexo:0\"]")).click();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:temFilhos\"]")).click();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:email\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:email\"]")).sendKeys("nome");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:nomeDoPai\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:nomeDoPai\"]")).sendKeys("a");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:nomeDaMae\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:nomeDaMae\"]")).sendKeys("a");
    new Select(driver.findElement(By.xpath("//*[@id=\"pessoaForm:estadoCivil\"]"))).selectByVisibleText("Casado(a)");
    
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:dataDeNascimento\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:dataDeNascimento\"]")).sendKeys("20/12/2013");
    new Select(driver.findElement(By.xpath("//*[@id=\"pessoaForm:raca\"]"))).selectByVisibleText("Preto");
    
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:cor\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:cor\"]")).sendKeys("Preto");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:pais\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:pais\"]")).sendKeys("v");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:municipioNaturalidade\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:municipioNaturalidade\"]")).sendKeys("sgdasgdsdga");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ufNat\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ufNat\"]")).sendKeys("a");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:cep\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:cep\"]")).sendKeys("6");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:logradouro\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:logradouro\"]")).sendKeys("2");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:bairro\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:bairro\"]")).sendKeys("ddd");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:numeroEnd\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:numeroEnd\"]")).sendKeys("988");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:complemento\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:complemento\"]")).sendKeys("das");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:municipioEnd\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:municipioEnd\"]")).sendKeys("a");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ufEnd\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ufEnd\"]")).sendKeys("ss");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:addEndereco\"]")).click();
    try {
      assertEquals("Endereco adicionado com sucesso", driver.findElement(By.xpath("/html/body/form/ul/li")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:tipo\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:tipo\"]")).sendKeys("cel");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ddd\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ddd\"]")).sendKeys("45");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:numero\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:numero\"]")).sendKeys("544");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ramal\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:ramal\"]")).sendKeys("0223452");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:addTelefone\"]")).click();
    try {
      assertEquals("Telefone adicionado com sucesso", driver.findElement(By.xpath("/html/body/form/ul/li")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:tipoDeDocumento\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:tipoDeDocumento\"]")).sendKeys("pdf");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:numeroDeDocumento\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:numeroDeDocumento\"]")).sendKeys("2");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:paisDoDocumento\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:paisDoDocumento\"]")).sendKeys("br");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:dataDeEmissao\"]")).clear();
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:dataDeEmissao\"]")).sendKeys("11/12/2013");
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:addDocumento\"]")).click();
    try {
      assertEquals("Documento adicionado com sucesso", driver.findElement(By.xpath("/html/body/form/ul/li")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.xpath("//*[@id=\"pessoaForm:salvar\"]")).click();
    try {
      assertEquals("Pessoa cadastrada com sucesso", driver.findElement(By.xpath("/html/body/form/ul/li")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
